#include "app.h"

#include <stddef.h> // 指针偏移计算
#include <stdio.h>

#include "base.h"

// 定义全局标记, 标记是否已经初始化， on_context_initialized 是否已经执行
int is_initialized = 0;

void CEF_CALLBACK on_before_command_line_processing(struct _cef_app_t *self, const cef_string_t *process_type,
                                                    struct _cef_command_line_t *command_line) {
    // 打印进程类型
    if (process_type) {
        cef_string_utf8_t cef_cmd_line_utf8 = {};
        cef_string_utf16_to_utf8(process_type->str, process_type->length, &cef_cmd_line_utf8);
        LOG_INFO("Process type: %s\n", cef_cmd_line_utf8.str);
        cef_string_utf8_clear(&cef_cmd_line_utf8);
    } else {
        LOG_INFO("Process type: Browser\n");
    }

    // 修改命令行参数（例如，禁用GPU）
    // cef_string_t sintch1 = {};
    // cef_string_utf8_to_utf16("disable-gpu", strlen("disable-gpu"), &sintch1);
    // cef_string_t sintch2 = {};
    // cef_string_utf8_to_utf16("disable-software-rasterizer", strlen("disable-software-rasterizer"), &sintch2);
    // cef_string_t sintch3 = {};
    // cef_string_utf8_to_utf16("disable-gpu-compositing", strlen("disable-gpu-compositing"), &sintch3);
    // cef_string_t sintch4 = {};
    // cef_string_utf8_to_utf16("in-process-gpu", strlen("in-process-gpu"), &sintch4);
    // command_line->append_switch(command_line, &sintch1);
    // command_line->append_switch(command_line, &sintch2);
    // command_line->append_switch(command_line, &sintch3);
    // command_line->append_switch(command_line, &sintch4);

    // cef_string_clear(&sintch1);
    // cef_string_clear(&sintch2);
    // cef_string_clear(&sintch3);
    // cef_string_clear(&sintch4);

    // 修改命令行参数（完全禁用GPU）
    // const char *disable_switches[] = {
    //     "disable-gpu",
    //     "disable-software-rasterizer",
    //     "disable-gpu-compositing",
    //     "disable-gpu-early-init", // 新增：阻止早期初始化
    //     "disable-gpu-sandbox",    // 新增：解除沙盒限制
    //     "use-gl=disabled"         // 新增：强制禁用GL
    // };

    const char *disable_switches[] = {"disable-gpu"};

    for (int i = 0; i < sizeof(disable_switches) / sizeof(disable_switches[0]); i++) {
        cef_string_t switch_str = {};
        const char *switch_name = disable_switches[i];
        cef_string_utf8_to_utf16(switch_name, strlen(switch_name), &switch_str);
        command_line->append_switch(command_line, &switch_str);
        cef_string_clear(&switch_str);
    }
}

void CEF_CALLBACK on_register_custom_schemes(struct _cef_app_t *self, struct _cef_scheme_registrar_t *registrar) {
    DEBUG_CALLBACK("log:   on_register_custom_schemes......................\n");
}

struct _cef_resource_bundle_handler_t *CEF_CALLBACK get_resource_bundle_handler(struct _cef_app_t *self) {
    DEBUG_CALLBACK("log:   get_resource_bundle_handler......................\n");
    // 返回NULL表示使用默认的资源包加载方式
    return NULL;
}

struct _cef_browser_process_handler_t *CEF_CALLBACK get_browser_process_handler(struct _cef_app_t *self) {
    DEBUG_CALLBACK("log:   get_browser_process_handler......................\n");
    // size_t offset = offsetof(app_ref_t, cef_app);
    // return &((app_ref_t *)((char *)self - offset))->browser_process_handler;
    // browser_app *app = (browser_app *)(((char *)self) - offsetof(browser_app, cef_app));
    browser_app *app = (browser_app *)self;
    return &app->browser_process_handler;
}

// struct _cef_render_process_handler_t *CEF_CALLBACK get_render_process_handler(struct _cef_app_t *self) {
//     // 返回NULL表示不使用自定义的渲染进程处理器
//     return NULL;
// }

void CEF_CALLBACK on_context_initialized(struct _cef_browser_process_handler_t *self) {
    is_initialized = 1;
    LOG_INFO("log:   on_context_initialized exec....................\n");
}

browser_app *browser_app_create_t() {
    browser_app *app = (browser_app *)calloc(1, sizeof(browser_app));

    // 初始化 cef_browser_process_handler_t
    app->browser_process_handler.base.size = sizeof(cef_browser_process_handler_t);
    app->browser_process_handler.on_context_initialized = on_context_initialized;

    // cef_app初始化
    app->cef_app.base.size = sizeof(cef_app_t);
    app->cef_app.on_before_command_line_processing = on_before_command_line_processing;
    app->cef_app.on_register_custom_schemes = on_register_custom_schemes;
    app->cef_app.get_resource_bundle_handler = get_resource_bundle_handler;
    app->cef_app.get_browser_process_handler = get_browser_process_handler;
    // app->cef_app.get_render_process_handler = get_render_process_handler;

    return app;
}